
package org.jetnuke.dao.generic;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.dao.generic.AutoKeys;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.DownloadsDownloads;

public class GenericDownloadDownloadDAO extends AutoKeys implements
		org.jetnuke.dao.DownloadDownloadDAO {

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadDownloadModel#retrieveByPK(int)
	 */
	public DownloadsDownloads retrieveByPK(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadDownloadModel.selectByID"));
		p.setInt(1, id);
		
		ResultSet rs = p.executeQuery();
		DownloadsDownloads s = new DownloadsDownloads();
		
		if (rs.next()) {
			fillDownDownFromResultSet(s, rs);
		}
		
		rs.close();
		p.close();

		return s;
	}

	/**
	 * @param s
	 * @param rs
	 */
	protected void fillDownDownFromResultSet(DownloadsDownloads s, ResultSet rs) throws Exception{
		s.setCID(rs.getInt("CID"));
		s.setDATE(rs.getTimestamp("DATE1"));
		s.setDESCRIPTION(rs.getString("DESCRIPTION"));
		s.setDOWNLOADRATINGSUMMARY(rs.getDouble("DOWNLOADRATINGSUMMARY"));
		s.setEMAIL(rs.getString("EMAIL"));
		s.setFILESIZE(rs.getInt("FILESIZE"));
		s.setHITS(rs.getInt("HITS"));
		s.setHOMEPAGE(rs.getString("HOMEPAGE"));
		s.setLID(rs.getInt("LID"));
		s.setNAME(rs.getString("NAME"));
		s.setSID(rs.getInt("SID"));
		s.setSUBMITTER(rs.getString("SUBMITTER"));
		s.setTITLE(rs.getString("TITLE"));
		s.setTOTALCOMMENTS(rs.getInt("TOTALCOMMENTS"));
		s.setTOTALVOTES(rs.getInt("TOTALVOTES"));
		s.setURL(rs.getString("URL"));
		s.setVERSION(rs.getString("VERSION"));
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadDownloadModel#selectAll()
	 */
	public List selectAll() throws Exception {
		PreparedStatement p;
		
		
		p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadDownloadModel.selectAll"));
			
			
		
		
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}
	protected List processSelectAll(ResultSet rs) throws Exception{
		List list = new ArrayList();
		while (rs.next()) {
			DownloadsDownloads u = new DownloadsDownloads();
			fillDownDownFromResultSet(u,rs);			
			list.add(u);
		}
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadDownloadModel#addNew(org.jetnuke.entities.DownloadsDownloads)
	 */
	public int addNew(DownloadsDownloads dc) throws Exception {
		PreparedStatement p = this.getStatementForAutoKeys("DownloadDownloadModel.addNew");
		this.setAutoGeneratedKeysQuery(SystemGlobals.getSql("DownloadDownloadModel.lastGeneratedDownloadID"));
		initNewDownload(dc,p);
		
		int id = this.executeAutoKeysQuery(p);
		p.close();		
		return id;
	}

	/**
	 * @param dc
	 * @param p
	 */
	private void initNewDownload(DownloadsDownloads dc, PreparedStatement p) throws Exception{
		p.setInt(1,dc.getCID());
		p.setTimestamp(2,new Timestamp(dc.getDATE().getTime()));
		p.setString(3,dc.getDESCRIPTION());
		p.setDouble(4,dc.getDOWNLOADRATINGSUMMARY());
		p.setString(5,dc.getEMAIL());
		p.setInt(6,dc.getFILESIZE());
		p.setInt(7,dc.getHITS());
		p.setString(8,dc.getHOMEPAGE());
		
		p.setString(9,dc.getNAME());
		p.setInt(10,dc.getSID());
		p.setString(11,dc.getSUBMITTER());
		p.setString(12,dc.getTITLE());
		p.setInt(13,dc.getTOTALCOMMENTS());
		p.setInt(14,dc.getTOTALVOTES());
		p.setString(15,dc.getURL());
		p.setString(16,dc.getVERSION());
		
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadDownloadModel#update(org.jetnuke.entities.DownloadsDownloads)
	 */
	public void update(DownloadsDownloads dc) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadDownloadModel.update"));
		
		p.setInt(1,dc.getCID());
		p.setTimestamp(2,new Timestamp(dc.getDATE().getTime()));
		p.setString(3,dc.getDESCRIPTION());
		p.setDouble(4,dc.getDOWNLOADRATINGSUMMARY());
		p.setString(5,dc.getEMAIL());
		p.setInt(6,dc.getFILESIZE());
		p.setInt(7,dc.getHITS());
		p.setString(8,dc.getHOMEPAGE());
		
		p.setString(9,dc.getNAME());
		p.setInt(10,dc.getSID());
		p.setString(11,dc.getSUBMITTER());
		p.setString(12,dc.getTITLE());
		p.setInt(13,dc.getTOTALCOMMENTS());
		p.setInt(14,dc.getTOTALVOTES());
		p.setString(15,dc.getURL());
		p.setString(16,dc.getVERSION());
		
		p.setInt(17,dc.getLID());
		p.executeUpdate();
		p.close();

	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadDownloadModel#countAll()
	 */
	public int countAll() throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadDownloadModel.countAll"));
		
		ResultSet rs = p.executeQuery();
		rs.next();
		int r= rs.getInt(1);
		rs.close();
		p.close();
		return r;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadDownloadModel#incHits(int)
	 */
	public void incHits(int id) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadDownloadModel.incHits"));
		p.setInt(1, id);
		
		p.executeUpdate();
		p.close();
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadDownloadModel#countUser(int, java.lang.String)
	 */
	public int countUser(int lid, String uname) throws Exception {
		PreparedStatement p;
		
		
		p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadDownloadModel.countUser"));
		p.setInt(1,lid);
		p.setString(2,uname);
		ResultSet rs = p.executeQuery();
		rs.next();
		
		int r= rs.getInt(1);
		rs.close();
		p.close();
		return r;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadDownloadModel#countAllByCategory(int)
	 */
	public int countAllByCategory(int cid) throws Exception {
		PreparedStatement p;
		
		
		p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadDownloadModel.countByCategory"));
		p.setInt(1,cid);
		ResultSet rs = p.executeQuery();
		rs.next();
		
		int r= rs.getInt(1);
		rs.close();
		p.close();
		return r;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadDownloadModel#getByHitsDown(int, int)
	 */
	public List getByHitsDown(int start, int limit) throws Exception {
		
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadDownloadModel.selectByHitsDown"));
		p.setInt(1,start);	
		p.setInt(2,limit);
			
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadDownloadModel#selectDateGreater(java.util.Date)
	 */
	public int selectDateGreater(Date dt) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadDownloadModel.selectDateGreater"));
		p.setTimestamp(1,new Timestamp(dt.getTime()));	
			
		ResultSet rs = p.executeQuery();
		rs.next();
		
		int r= rs.getInt(1);
		rs.close();
		p.close();
		return r;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadDownloadModel#countWithRating()
	 */
	public int countWithRating() throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadDownloadModel.countWithRating"));
		
		ResultSet rs = p.executeQuery();
		rs.next();
		int r= rs.getInt(1);
		rs.close();
		p.close();
		return r;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadDownloadModel#selectWithRating()
	 */
	public List selectWithRating(int totalVotesMin,int limit) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadDownloadModel.selectWithRating"));
		p.setInt(1,totalVotesMin);	
		p.setInt(2,limit);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadDownloadModel#selectTitleAsc(int, int, int)
	 */
	public List selectTitleAsc(int cid, int start, int offset) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadDownloadModel.selectTitleAsc"));
		p.setInt(1,cid);	
		p.setInt(2,start);
		p.setInt(3,offset);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadDownloadModel#selectTitleDesc(int, int, int)
	 */
	public List selectTitleDesc(int cid, int start, int offset) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadDownloadModel.selectTitleDesc"));
		p.setInt(1,cid);	
		p.setInt(2,start);
		p.setInt(3,offset);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;

	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadDownloadModel#selectDateAsc(int, int, int)
	 */
	public List selectDateAsc(int cid, int start, int rowcount) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadDownloadModel.selectDateAsc"));
		p.setInt(1,cid);	
		p.setInt(2,start);
		p.setInt(3,rowcount);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;

	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadDownloadModel#selectDateDesc(int, int, int)
	 */
	public List selectDateDesc(int cid, int start, int offset) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadDownloadModel.selectDateDesc"));
		p.setInt(1,cid);	
		p.setInt(2,start);
		p.setInt(3,offset);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;

	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadDownloadModel#selectRatingAsc(int, int, int)
	 */
	public List selectRatingAsc(int cid, int start, int offset) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadDownloadModel.selectRatingAsc"));
		p.setInt(1,cid);	
		p.setInt(2,start);
		p.setInt(3,offset);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;

	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadDownloadModel#selectRatingDesc(int, int, int)
	 */
	public List selectRatingDesc(int cid, int start, int offset) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadDownloadModel.selectRatingDesc"));
		p.setInt(1,cid);	
		p.setInt(2,start);
		p.setInt(3,offset);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadDownloadModel#selectHitsAsc(int, int, int)
	 */
	public List selectHitsAsc(int cid, int start, int offset) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadDownloadModel.selectHitsAsc"));
		p.setInt(1,cid);	
		p.setInt(2,start);
		p.setInt(3,offset);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadDownloadModel#selectHitsDesc(int, int, int)
	 */
	public List selectHitsDesc(int cid, int start, int offset) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadDownloadModel.selectHitsDesc"));
		p.setInt(1,cid);	
		p.setInt(2,start);
		p.setInt(3,offset);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;

	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadDownloadModel#selectTopDown(int)
	 */
	public List selectTopDown(int limit) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadDownloadModel.selectTopDown"));
		p.setInt(1,limit);	
		
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	/* (non-Javadoc)
	 * @see org.jetnuke.model.DownloadDownloadModel#search(java.lang.String, java.lang.String)
	 */
	public List search(String title, String description,int start, int rowcount) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadDownloadModel.search"));
		p.setString(1,title);	
		p.setString(2,description);
		p.setInt(3,start);	
		p.setInt(4,rowcount);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();
		
		return list;
	}

	public void deleteByLID(int lid) throws Exception {
		PreparedStatement p = JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadDownloadModel.deleteByLID"));
		
		p.setInt(1,lid);
		p.executeUpdate();
		p.close();
		
	}

	public int countSearch(String title, String description) throws Exception {
		PreparedStatement p= JForumExecutionContext.getConnection().prepareStatement(SystemGlobals.getSql("DownloadDownloadModel.countSearch"));
		p.setString(1,title);
		p.setString(2,description);
		ResultSet rs = p.executeQuery();
		rs.next();
		int r= rs.getInt(1);
		rs.close();
		p.close();
		return r;
	}

}
